package com.fattyCorps.game.util.fsm;

import java.util.HashMap;

public class FsmMch {
    HashMap<String, Object> blackboard = new HashMap<>();
    HashMap<Class, IFsmState> states = new HashMap<>();

    IFsmState curState = null;

    public void addState(IFsmState state) {
        Class cls = state.getClass();
        if (states.containsKey(cls)) {
            return;
        }

        state.setMch(this);
        states.put(cls, state);
    }

    public void setState(Class cls) {
        IFsmState state = states.get(cls);
        if (state == null) {
            throw new RuntimeException("无效的状态");
        }

        if (curState != null) {
            curState.onExit();
        }

        state.onEnter();
        curState = state;
    }

    public void addBlackboard(String key, Object val) {
        blackboard.put(key, val);
    }

    public Object getBlackboard(String key) {
        return blackboard.get(key);
    }
}
